import { useState, useMemo } from 'react'

export const useCount = initData => {
    const [count, setCount] = useState(initData)

    const [increase, decrease] = useMemo(() => {
        const increase = () => {
            setCount((latestCount) => latestCount + 1)
        }

        const decrease = () => {
            setCount((latestCount) => latestCount - 1)
        }

        return [increase, decrease]
    }, []) // 保持依赖数组为空，这样 increase 和 decrease 方法都只会被创建一次

    return [count, increase, decrease]
}